package edu.sjsu.cs.queryopt.dbms.dao;

import java.text.DecimalFormat;
import java.util.ArrayList;

public class Select {

	PlanTable planTable;
	ArrayList<Predicate> predicates;
	ArrayList<Table> tables;

	public Select() {
		planTable = new PlanTable();
		predicates = new ArrayList<Predicate>();
		tables = new ArrayList<Table>();
	}

	public Select(PlanTable planTable, ArrayList<Predicate> predicates,
			ArrayList<Table> tables) {
		this.planTable = planTable;
		this.predicates = predicates;
		this.tables = tables;
	}

	/**
	 * Use this method to print all the values you need for the select.
	 */
	public void printSelect() {
		System.out.println("\nPrinting Select output ...\n");
		printTableIndexes();
		printPlanTable();
		printPredicateTable();
	}

	/**
	 * Prints each table's index information.
	 */
	private void printTableIndexes() {

		for (Table table : tables) {
			System.out.println("Index table for " + table.name);
			int maxCols = 0;
			for (Index index : table.indexes) {
				if (maxCols < index.definitions.size())
					maxCols = index.definitions.size();
			}

			System.out.print("\nIndex Name   ");
			for (int i = 0; i < maxCols; i++)
				System.out.print("COL" + i + "  ");
			System.out.print("HighKey\t\t");
			System.out.print("LowKey\t\t");

			System.out.print("\n-------------");
			for (int i = 0; i < maxCols; i++)
				System.out.print("------");

			for (Index index : table.indexes) {
				System.out.format("\n%-13s", index.name);

				for (String definition : index.definitions) {
					System.out.format("%-6s", definition);
				}

//				System.out.format("%-40s", index.highKey);
//				System.out.format("%-40s", index.lowKey);
			}
			System.out.println();
		}

	}

	/**
	 * Prints Access Plan information collected for the query.
	 */
	private void printPlanTable() {

		System.out.println("Plan Table \t\tValue");
		System.out.println("--------------------------------");
		System.out.println("QBlockNo \t\t" + planTable.queryBlockNo);
		System.out.println("AccessType \t\t" + planTable.accessType);
		System.out.println("MatchCols \t\t" + planTable.matchCols);
		System.out.println("AccessName \t\t" + planTable.accessName);
		System.out.println("IndexOnly \t\t" + planTable.indexOnly);
		System.out.println("Prefetch \t\t" + planTable.prefetch);
		System.out.println("SortC_OrderBy \t\t" + planTable.sortC_orderBy);
		System.out.println("Table1Card \t\t" + planTable.table1Card);

		if (planTable.table2Card == 0)
			System.out.println("Table2Card \t\t");
		else
			System.out.println("Table2Card \t\t" + planTable.table2Card);

		if (planTable.leadTable != null)
			System.out.println("LeadingTable \t\t" + planTable.leadTable);
		else
			System.out.println("LeadingTable \t\t ");

		System.out.println();

	}

	/**
	 * Prints prediate table for each predicate.
	 */
	private void printPredicateTable() {

		System.out
				.println("Predicate Table \tType \tC1 \tC2 \tFF1 \tFF2 \tSeq \tText \t\tDescription");
		System.out
				.println("----------------------------------------------------------------------------------------------------");

		DecimalFormat df = new DecimalFormat("#.####");
		int i = 0;
		if (predicates != null && predicates.size() != 0) {
			for (Predicate predicate : predicates) {
				String output = "PredNo" + i + "\t\t\t" + predicate.type + "\t"
						+ predicate.card1 + "\t";

				if (predicate.card2 != 0)
					output += predicate.card2;
				else
					output += " ";

				output += "\t";

				if (predicate.ff1 > 1)
					output += df.format(1.00);
				else if (predicate.ff1 < 0)
					output += df.format(0.00);
				else
					output += df.format(predicate.ff1);

				output += "\t";

				if (predicate.ff2 > 1)
					output += df.format(1.00);
				else if (predicate.ff1 < 0)
					output += df.format(0.00);
				else if (predicate.ff2 != 0)
					output += df.format(predicate.ff2);
				else
					output += " ";

				output += "\t" + predicate.sequence + "\t" + predicate.text
						+ "\t\t" + predicate.description;

				System.out.println(output);
				i++;
			}
		}
	}
}
